﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Comet.Net;
using System.Net;
using System.IO;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Net.NetworkInformation;

namespace Comet.Samples.Time {
    class Program {

		#region Methods (5) 


		// Private Methods (5) 

        private static Sample GetSampleChoice (string[] args) {
            if (args.Length == 0 || string.IsNullOrEmpty (args[0]))
                return Sample.Undefined;
            try {
                return (Sample)Enum.Parse (typeof (Sample), args[0], true);
            }
            catch {
                return Sample.Undefined;
            }
        }

        private static CometNetHandler GetSampleHandler (Sample sample, List<string> options) {
            switch (sample) {
                case Sample.Time:
                    return Time (options);
                default:
                    throw new NotSupportedException ();
            }
        }

        static void Main (string[] args) {
            try {
                Console.WriteLine ("Comet Samples");
                Console.WriteLine ("from the Comet (http://codeplex.com/comet) open-source project");
                if (!CometListener.IsSupported) {
                    Console.WriteLine ("\r\nNOTE: Can not run on this OS / Service Pack");
                }
                if (!NetworkInterface.GetIsNetworkAvailable ()) {
                    Console.WriteLine ("\r\nNOTE: No network connection available.");
                }
                Sample sample = GetSampleChoice (args);

                switch (sample) {
                    case Sample.Undefined:
                        Console.WriteLine ("\r\ncomets.exe [sample] [options] [uriPrefixes]");
                        Console.WriteLine ("\r\n{Time Sample}:");
                        Console.WriteLine ("\t time [timeZone] [uriPrefixes]");
                        Console.WriteLine ("\t\t [timeZone] a system timezone (ex: Eastern Standard Time)");
                        Console.WriteLine ("\r\n{Common}:");
                        Console.WriteLine ("\r\n\t [uriPrefix(ex)] one or more uri prefixes");
                        Console.WriteLine ("\t                 (ex: http://localhost/sample/)");
                        break;
                    default:
                        List<string> options = new List<string>();
                        using (CometListener cometListener = new CometListener ()) {
                            ProcessArgs (args.Skip(1), options, cometListener.Prefixes);
                            Console.WriteLine ("\r\nListening on:");
                            foreach (var prefix in cometListener.Prefixes) {
                                Console.WriteLine ("\t{0}", prefix);
                            }
                            cometListener.Start (GetSampleHandler (sample, options), 30);
                            Console.ReadLine ();
                        }
                        break;
                }
            }
            catch (Exception ex) {
                Console.WriteLine ("\r\nERROR:");
                Console.WriteLine (ex.ToString ());
            }
        }

        private static void ProcessArgs (IEnumerable<string> args, List<string> options, HttpListenerPrefixCollection prefixes) {
            foreach (string arg in args)
                if (Uri.IsWellFormedUriString (arg, UriKind.Absolute))
                    prefixes.Add (arg);
                else
                    options.Add (arg);

            if (prefixes.Count == 0)
                prefixes.Add ("http://localhost/comet/sample/");
        }

        private static CometNetHandler Time (List<string> options) {
            TimeZoneInfo timeZoneInfo = TimeZoneInfo.Local;
            if (options.Count > 0)
                try {
                    timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById (options[0]);
                }
                catch {
                    // ignore
                }
            Console.WriteLine ("Timezone: {0}", timeZoneInfo.StandardName);
            return new CometTimeHandler (timeZoneInfo);
        }


		#endregion Methods 

    }
}